import { brands } from './brand'
import { deleteAction, getAction, postAction, downFile, getFileAccessHttpUrl } from '@/api/manage'
import { pattern } from '@api/pattern/index'
export const listMixin = {
  data() {
    return {
      dataSource: [],
      brands,
      form: this.$form.createForm(this),
      name:'',
      code:'',
      // current: 1,
      // pageSize: 10,
      brand:undefined,
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40'],
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: total => `共 ${total} 条记录`,
        itemRender: (current, type, originalElement) => {
          if (type === 'prev') {
            return <a>上一页</a>
          } else if (type === 'next') {
            return <a>下一页</a>
          }
          return originalElement
        }
      }
    }
  },
  created() {},
  mounted() {
    this.loadData()
  },
  computed: {
    jobNumber() {
      return this.$store.getters.userInfo.workNo || ''
    }
  },
  methods: {
    handleSubmit(e) {
      e.preventDefault()
      this.current = 1
      this.loadData()
    },
    resetForm() {
      console.log('重置表达')
      this.brand = ''
      this.form.resetFields()
      this.loadData(1)
    },
    searchBrand(weid) {
      const brand = this.brands.filter(item => item.weid == weid)
      if (brand.length > 0) {
        return brand[0]
      } else {
        return { name: '' }
      }
    },
    getQueryParams() {
      const params = this.form.getFieldsValue()
      console.log(params, '查询参数')
      console.log(this.$route.query)
      const size = this.pageSize
      // const { pageSize: size } = this.pagination
      return {
        ...params,
        size
      }
    },
    handleTableChange(pagination) {
      console.log('gaibian', pagination)
      this.pagination = pagination
      this.loadData()
    },
    reCalculatePage(count) {
      //总数量-count
      let total = this.pagination.total - count
      //获取删除后的分页数
      let currentIndex = Math.ceil(total / this.pagination.pageSize)
      //删除后的分页数<所在当前页
      if (currentIndex < this.pagination.current) {
        this.pagination.current = currentIndex
      }
      console.log('currentIndex', currentIndex)
    },
    deleteRecord(record,num) {
      if (!this.urlKey || this.urlKey == '') {
        return this.$message.error('请设置urlKey属性!')
      }
      this.$confirm({
        title: '确定删除吗?',
        cancelText: '暂不',
        onOk: res => {
          console.log(res)
          return new Promise((resolve, reject) => {
            if(num == 1){
              postAction(`${pattern}comments/delete`,{ commentsId: record.commentsId,jobNumber:this.jobNumber }).then(res => {
                if (res.code == 200) {
                  this.deleteEvent(record.id,1)
                  this.$message.success('删除成功')
                  resolve()
                } else {
                  this.$message.success(res.msg)
                  reject()
                }
              }).catch(err => {
                resolve()
                console.log(err)
              })
            }else  if(num == 2){
              postAction(`${pattern}patent/delete`,{ patentId: record.patentId,jobNumber:this.jobNumber }).then(res => {
                if (res.code == 200) {
                  this.deleteEvent(record.id,2)
                  this.$message.success('删除成功')
                  resolve()
                } else {
                  this.$message.success(res.msg)
                  reject()
                }
              }).catch(err => {
                resolve()
                console.log(err)
              })
            }else{
              postAction(`${pattern}${this.urlKey}/delete`,{ id: record.id,jobNumber:this.jobNumber }).then(res => {
                console.log(res)
                if (res.code == 200) {
                  this.visible = false
                  // this.reCalculatePage(1)
                  // this.loadData(1)
                  this.deleteEvent(record.id)
                  this.$message.success('删除成功')
                  resolve()
                } else {
                  this.$message.success(res.msg)
                  reject()
                }
              }).catch(err => {
                resolve()
                console.log(err)
              })
            } 
          }).catch(() => console.log('Oops errors!'))
        },
        onCancel() {}
      })
    },
    deleteEvent(id,num) {
     if(num){
      // this.dataSource.map((item, index) => {
      //   if (item.id == id) {
      //     item.commentsId = 0
      //   }
      // })
      this.loadData()
     }else{
      this.dataSource.map((item, index) => {
        if (item.id == id) {
          this.dataSource.splice(index, 1)
        }
      })
     }
    },
    // 加载更多
    onLoadMore() {
      this.loadingMore = true
      this.current += 1
      this.loadData(2)
    },
    loadData(arg) {
      if (!this.urlKey || this.urlKey == '') {
        this.$message.error('请设置url.list属性!')
        return
      }
    
      var params = this.getQueryParams() //查询条件
      if (this.patternId) {
        params.patternId = this.patternId
      }
       //加载数据 若传入参数1则加载第一页的内容
     if (arg === 2) {
      params.current = this.current
    }else{
      params.current = 1
      this.form.resetFields();
    }
    if (this.jobNumber) {
      params.jobNumber = this.jobNumber
    }
    if(this.season){
      params.season = this.season
    }
    if(this.brand){
      params.brand = this.brand
    }
    if(this.categoryName){
      params.categoryName = this.categoryName
    }
    if(this.name){
      params.name = this.name
    }
    if(this.code){
      params.code = this.code
    }
    if(this.patentFlag){
      params.patentFlag = this.patentFlag
    }
    if(this.commentsFlag){
      params.commentsFlag = this.commentsFlag
    }
     try {
      postAction(`${pattern}${this.urlKey}/list`, params).then(res => {
        if (res.code == 200) {
          this.current = res.data.current
          this.currentTotal = this.current * this.pageSize
          const dataList = res.data.records
          this.dataSource = res.data.current === 1? res.data.records : this.dataSource.concat(dataList)
          if (res.data.total > res.data.size) {
            this.showLoadingMore = true
          } else {
            this.showLoadingMore = false
          }
        
        } else {
          this.$message.warning(res.msg)
        }  
        this.loadingMore = false
        this.loading = false
      })
     } catch (error) {
      this.loadingMore = false
      this.loading = false
     }
    }
  }
}
